package com.ghbank.rdws.codeservice.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.commons.lang.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.ghbank.rdws.biz.enums.FetchName;
import com.ghbank.rdws.biz.service.RdwsService;
import com.ghbank.rdws.codeservice.dto.CodeMappingDTO;
import com.ghbank.rdws.codeservice.dto.CodeTypeDTO;
import com.ghbank.rdws.common.http.GhbankResponseEntity;
import com.ghbank.rdws.codeservice.dto.SystemInfoDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(tags = "码值控制器")
@RestController
@Slf4j
public class CodeController {
    @Autowired
    private RdwsService rdwsService;
    
    @ApiOperation(value = "获取码值映射", notes = "根据码值类型获取码值映射")
    @ApiImplicitParam(name = "codeTypeDTO",value = "码值",required = true,dataType = "CodeTypeDTO")
    @PostMapping(value = "/rdws_code/code_mapping")
    public GhbankResponseEntity<List<CodeMappingDTO>> getMappingInfo(@Valid @RequestBody CodeTypeDTO codeTypeDTO) {
        List<Map<String,Object>> result = null;
        String businessType = codeTypeDTO.getBusinessType();
        String seq = codeTypeDTO.getSeq();
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("businessType", businessType);
            result = rdwsService.queryForList(FetchName.CODE_MAPPING.getName(), paramMap);
            Map<String,CodeMappingDTO> map = new HashMap<String,CodeMappingDTO>();
            List<CodeMappingDTO> codeMappings = new ArrayList<CodeMappingDTO>();
            //合并码值相同数据
            for (Map<String, Object> data : result) {
                String code = ObjectUtils.toString(data.get("standard_code"));
                String name = ObjectUtils.toString(data.get("standard_name"));
                String sysType = ObjectUtils.toString(data.get("sys_type"));
                String sysTypeCode = ObjectUtils.toString(data.get("mapping_code"));
                CodeMappingDTO codeMappingDTO = map.get(code);
                if (codeMappingDTO == null) {
                    codeMappingDTO = new CodeMappingDTO();
                    codeMappingDTO.setCode(code);
                    codeMappingDTO.setName(name);
                    SystemInfoDTO systemInfoDTO = new SystemInfoDTO();
                    systemInfoDTO.setSysType(sysType);
                    systemInfoDTO.setSysTypeCode(sysTypeCode);
                    codeMappingDTO.getSystems().add(systemInfoDTO);
                } else {
                    SystemInfoDTO systemInfoDTO = new SystemInfoDTO();
                    systemInfoDTO.setSysType(sysType);
                    systemInfoDTO.setSysTypeCode(sysTypeCode);
                    codeMappingDTO.getSystems().add(systemInfoDTO);
                }
                map.put(code, codeMappingDTO);
            }
            for (CodeMappingDTO codeMappingDTO : map.values()) {
                codeMappings.add(codeMappingDTO);
            }
            return GhbankResponseEntity.ok(codeMappings,seq);
        } catch (Exception e){
            log.error(e.getMessage(),e);
            return GhbankResponseEntity.error(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage(),seq);
        }
    }
}
